package com.jdiv.util;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsEnvironment;
import java.awt.Transparency;
import java.awt.image.BufferedImage;

/**
 * @author  Joyal
 */
public class MaskColorImage {

 private static Color defaultMaskColor=new Color(0,0,0);
 private static Color maskColor=new Color(0,0,0);
 private static Color replaceColor=new Color(0,0,0);
 private static Color replaceNewColor=new Color(0,0,0);
 private static boolean replace=false;
// private static Color maskColor=new Color(255,236,232);
 public MaskColorImage(){}
 
 /**
 * @param maskColor
 * @uml.property  name="maskColor"
 */
public static void setMaskColor(Color maskColor){
	// aqui colocarian el color que 
     //quieren colocar transparente en la forma R,G,B

	 MaskColorImage.maskColor=maskColor;
 }
 
 public static void replaceColor(Color color,Color newColor){
	 replaceColor=color;
	 replaceNewColor=newColor;
	 replace=true;
 }
 public static BufferedImage maskImage(BufferedImage bmp){
	// "bmp" este seria el archivo de imagen original a quien quiere darle
	// la transparencia al pixel, que se lo pasar por parametro

	        GraphicsConfiguration CONFIG =GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
								  
			BufferedImage image = CONFIG.createCompatibleImage(bmp.getWidth(),bmp.getHeight(),Transparency.BITMASK);
			
	                Graphics2D g2 = image.createGraphics();
			g2.setComposite(AlphaComposite.Src);
			g2.drawImage(bmp, 0, 0, null);
			g2.dispose();

			for (int y=0;y < image.getHeight();y++)
			for (int x=0;x < image.getWidth();x++) {
				int color = image.getRGB(x, y);
				if (color == maskColor.getRGB()) {
					image.setRGB(x, y, color & 0x00ffffff);
					
					//Remplaza el color si esta activado
				}else if (replace && color == replaceColor.getRGB()) {
					image.setRGB(x, y, replaceNewColor.getRGB());
				}
			}
	  return image; //la imagen final con las transparencias 
 }
 
}
